<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<th:block th:include="include :: header('用户分配角色')" />
</head>
<body>
	<div class="main-content">
		<form id="form-user-add" class="form-horizontal">
			<input type="hidden" id="userId" name="userId"
				th:value="${user.userId}">
			<h4 class="form-header h4">基本信息</h4>
			<div class="row">
				<div class="col-sm-6">
					<div class="form-group">
						<label class="col-sm-4 control-label is-required">用户名称：</label>
						<div class="col-sm-8">
							<input name="userName" class="form-control" type="text" disabled
								th:value="${user.userName}">
						</div>
					</div>
				</div>
				<div class="col-sm-6">
					<div class="form-group">
						<label class="col-sm-4 control-label is-required">登录账号：</label>
						<div class="col-sm-8">
							<input name="loginName" class="form-control" type="text" disabled
								th:value="${user.loginName}">
						</div>
					</div>
				</div>
			</div>

			<h4 class="form-header h4">分配角色</h4>
			<div class="row">
				<div class="col-sm-12">
					<div class="col-sm-12 select-table table-striped">
						<table id="bootstrap-table"></table>
					</div>
				</div>
			</div>
		</form>
	</div>

	<div class="row">
		<div class="col-sm-offset-5 col-sm-10">
			<button type="button" class="btn btn-sm btn-primary"
				onclick="submitHandler()">
				<i class="fa fa-check"></i>保 存
			</button>
			&nbsp;
			<button type="button" class="btn btn-sm btn-danger"
				onclick="closeItem()">
				<i class="fa fa-reply-all"></i>关 闭
			</button>
		</div>
	</div>
	<th:block th:include="include :: footer" />
	<script th:inline="javascript">
	    var prefix = ctx + "system/user/authRole";
	    var userRoles = [[${userRoles}]]
	    
	    $(function() {
		    var options = {
		        url: ctx + "system/role/list",
		        sortName: "roleSort",
		        showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                clickToSelect: true,
		        columns: [{
		            checkbox: true,
		            formatter:function (value, row, index) {
		            	for (var i = 0; i < userRoles.length; i++) {
		            	    if (userRoles[i].roleId == row.roleId) {
		            	        return { checked: true };
		            	    }
		            	}
		            	return { checked: false };
		            }
		        },
		        {
		            field: 'roleId',
		            title: '角色编号'
		        },
		        {
		            field: 'roleName',
		            title: '角色名称',
		            sortable: true
		        },
		        {
		            field: 'roleKey',
		            title: '权限字符',
		            sortable: true
		        },
		        {
		            field: 'createTime',
		            title: '创建时间',
		            sortable: true
		        }]
		    };
		    $.table.init(options);
		});
	    
	    /* 添加角色-提交 */
        function submitHandler(index, layero){
        	var rows = $.table.selectFirstColumns();
        	var data = { "userId": $("#userId").val(), "roleIds": rows.join() };
			$.operate.saveTab(prefix + "/insertAuthRole", data);
        }
    </script>
</body>
</html>